﻿using System;
using System.Linq;
using System.Threading.Tasks;
using StyleMVVM.ViewModel;
using Wala.Connection.Objects.Profiles;
using StyleMVVM.View;
using StyleMVVM.DependencyInjection;
using Wala.Infrastructure;
using Windows.UI.Popups;
using Windows.UI.Xaml.Data;
using System.Collections.ObjectModel;
using Wala.Connection.Objects.Contacts;
using System.Collections.Generic;
using Wala.Connection.Managements;
using System.ComponentModel;
using Windows.UI.Xaml.Controls;
using Wala.Connection.Constants;
using Wala.Connection.Helpers;
using Wala.Connection.Objects.Authen;
using Wala.Connection.ComConnections;

namespace Wala.ViewModels.Main
{
    public class MainPageViewModel : PageViewModel,INotifyPropertyChanged
    {
        #region //------------- Private Fields ------------//

        public event PropertyChangedEventHandler PropertyChanged;

        private ShortProfile profile;
        private bool isBusy;
        private ObservableCollection<ContactBasic> contacts;

        private EventCommand itemClickCommand;

        #endregion

        #region //------------ CLR Properties -------------//

        public string DisplayName
        {
            get
            {
                if (profile != null)
                {
                    return profile.Display_name;
                }
                return "";
            }
        }

        public string AvatarUrl
        {
            get 
            {
                if (profile != null)
                {
                    return string.Format(URLConstant.GET_AVATAR, profile.Avatar_id);
                }
                return "";
            }
        }

        public ObservableCollection<ContactBasic> Contacts
        {
            get 
            { 
                return contacts; 
            }
            set 
            {
                if (contacts != value)
                {
                    contacts = value;
                    OnPropertyChanged("Contacts");
                }
            }
        }

        public bool IsBusy
        {
            get { return isBusy; }
            set 
            {
                if (this.isBusy != value)
                {
                    isBusy = value;
                    OnPropertyChanged("IsBusy");
                }
            }
        }

        public EventCommand ItemClickCommand
        {
            get
            {
                if (itemClickCommand == null)
                {
                    itemClickCommand = new EventCommand(itemClickHandler);
                }
                return itemClickCommand;
            }
        }

        #endregion

        #region //------------- Constructors ------------//

        public MainPageViewModel()
        {
            
        }

        [ActivationComplete]
        public async void Active()
        {
            var objectStorageHelper = new StorageHelper<RetAuthen>(StorageType.Local);
            RetAuthen retAuthen = await objectStorageHelper.LoadASync("RetAuthen");
            ConnectionManagement conn = ConnectionManagement.Instance;
            conn.MainViewMessageTransfered += new MessageTransferedEventHanler(ShowMessage);
            Client.LoadContact(retAuthen.User.Id);

            IsBusy = true;

            try
            {
                
                List<ContactBasic> contactBasics = await ContactManager.GetContactBasicList();
                Contacts = new ObservableCollection<ContactBasic>();
                foreach (ContactBasic contact in contactBasics)
                {
                    Contacts.Add(contact);
                }
            }
            catch (Exception exp)
            {
                Log.Error("Exception thrown while fetching contacts", exp);
            }
            finally
            {
                IsBusy = false;
            }
        }        

        #endregion
        #region //------------ Private Methods ------------//

        private void ShowMessage(object sender, Connection.Argument.NotifyEventArgs notifyEventArgs)
        {
            switch (notifyEventArgs.Code)
            {
                case EventConstants.RET_LOAD_PRESENCE_SUCCESS_MSG:
                    MessageDialog m = new MessageDialog("a");
                    m.ShowAsync();
                    break;

                case EventConstants.RET_LOAD_PRESENCE_ERROR_MSG:
                    break;
            }
        }

        private void itemClickHandler(object sender, object args, object parameter)
        {
            ItemClickEventArgs clickEventArgs = args as ItemClickEventArgs;

            if (clickEventArgs != null)
            {                
                Navigation.Navigate("FriendCommonPage", (ContactBasic)clickEventArgs.ClickedItem);
            }
            else
            {
                Log.Error("Missed ItemClickEventArgs args");
            }
        }

        #endregion

        #region //--------- Override Methods ------------//

        protected override void OnNavigatedTo(object sender, StyleNavigationEventArgs e)
        {
            profile = NavigationParameter as ShortProfile;            
        }
        #endregion

        #region //----------- Protect Methods ----------//

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}